﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.IO;
using SMS_Server;

namespace SMS_Server_Manager.Forms
{
    public partial class ChangeModem : Form
    {
        public ChangeModem()
        {
            InitializeComponent();
        }

        private Form Sender;

        public System.Windows.Forms.DialogResult ViewDialog(Form sender)
        {
            Sender = sender;
            return this.ShowDialog(sender);
        }

        private void ChangeModem_Load(object sender, EventArgs e)
        {
            ChangeButton.Enabled = false;
            try
            {
                TcpClient tcp = new TcpClient();
                tcp.Connect(Global.GetDefaultIP(), 2000);
                NetworkStream ns = tcp.GetStream();
                StreamReader sr = new StreamReader(ns);
                StreamWriter sw = new StreamWriter(ns);
                //lets get the device details.
                sw.WriteLine("CD");
                sw.Flush();
                string devicedetails = sr.ReadLine();
                GSMModem cmodem = null;
                if (devicedetails != "")
                {
                    string[] namesplit=devicedetails.Split(',');
                    cmodem = new GSMModem(namesplit[0], namesplit[3], namesplit[2], namesplit[1]);
                    ModemList.Items.Add(cmodem);
                    ModemList.SelectedIndex = 0;
                }
                //Get list of devices
                sw.WriteLine("DL");
                sw.Flush();
                devicedetails = sr.ReadLine(); if (devicedetails != "")
                {
                    string[] devices = devicedetails.Split('|');
                    foreach (string device in devices)
                    {
                        string[] namesplit = device.Split(',');
                        GSMModem modem = new GSMModem(namesplit[0], namesplit[3], namesplit[2], namesplit[1]);
                        if (cmodem == null)
                            ModemList.Items.Add(modem);
                        else
                        {
                            if (modem.IMEINumber != cmodem.IMEINumber)
                                ModemList.Items.Add(modem);
                        }
                    }
                }
                tcp.Close();
            }
            catch
            {
                ChangeButton.Enabled = false;
            }
        }

        private void ModemList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ModemList.SelectedIndex < 0)
            {
                ChangeButton.Enabled = false;
            }
            else
            {
                ChangeButton.Enabled = true;
            }
        }

        private void CancleButton_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void ChangeButton_Click(object sender, EventArgs e)
        {
            ((Mainform)Sender).newModemName.Text = "New: " + ((GSMModem)ModemList.SelectedItem).ToString();
            Global.IMEI = ((GSMModem)ModemList.SelectedItem).IMEINumber;
            //lets save to database
            DataAccess DA = new DataAccess(Global.GetConfigurationDatabaseName(), Global.DatabasePassword);
            //update isvalidserial
            DA.CommandString = "Update General_Config SET ParamValue=@ParamValue WHERE ParamName=@ParamName;";
            DA.AddParameter("@ParamValue", Global.IMEI);
            DA.AddParameter("@ParamName", "IMEI");
            DA.ExecuteNonQuery();
            DA.Close();
            Global.unsaved_changes = true;
            DialogResult = System.Windows.Forms.DialogResult.OK;
        }
    
    }
}
